using System;
using System.Collections.Generic;
using System.Text;

namespace Pegasus.DirectoryServices
{
	/// <summary>
	/// Class is used to change and inspect a windows username.
	/// </summary>
	/// <remarks>
	/// A username can be in one of the standard formats
	///		bnelson				
	///		PEGASUS\bnelson
	/// </remarks>
	public class Username
	{
		// Local Instance Values;
		private string m_domain = string.Empty;
		private string m_user = string.Empty;

		/// <summary>
		/// Initializes a new instance of the <see cref="T:Username"/> class.
		/// </summary>
		/// <param name="username">The username.</param>
		public Username( string username )
		{
			m_domain = GetDomain( username );
			m_user = GetUsername( username );
		}

		/// <summary>
		/// Gets the domain.
		/// </summary>
		/// <value>The domain.</value>
		public string Domain
		{
			get
			{
				return m_domain;
			}
		}

		/// <summary>
		/// Gets the user.
		/// </summary>
		/// <value>The user.</value>
		public string User
		{
			get
			{
				return m_user;
			}
		}

		/// <summary>
		/// Returns a <see cref="T:System.String"></see> that represents the current <see cref="T:System.Object"></see>.
		/// </summary>
		/// <returns>
		/// A <see cref="T:System.String"></see> that represents the current <see cref="T:System.Object"></see>.
		/// </returns>
		public override string ToString()
		{
			return string.Format( "{0}\\{1}", m_domain, m_user );
		}

		/// <summary>
		/// Gets the username.
		/// </summary>
		/// <param name="username">The username.</param>
		/// <returns></returns>
		public static string GetUsername( string username )
		{
			int pos = username.IndexOf( "\\" );
			if( pos > -1 )
			{
				return username.Substring( pos + 1 );
			}

			return username;
		}

		/// <summary>
		/// Gets the domain.
		/// </summary>
		/// <param name="username">The username.</param>
		/// <returns></returns>
		public static string GetDomain( string username )
		{
			int pos = username.IndexOf( "\\" );
			if( pos > -1 )
			{
				return username.Substring( 0, pos );
			}

			return string.Empty;
		}
	}
}
